package cn.wizzer.app.web.commons.utils;

import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import org.nutz.integration.jedis.RedisService;
import org.nutz.lang.Strings;
import redis.clients.jedis.ScanParams;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class RedisUtil {

    private static RedisService redisService;

    public static void init(RedisService redisService) {
        RedisUtil.redisService = redisService;
    }

    public static <T> T get(Class clzz, String key) {
        return JSONUtil.parseStringToJavaBean(clzz, redisService.get(key));
    }

    public static String get(String key) {
        return redisService.get(key);
    }

    public static void set(String key, Object value) {
        redisService.set(key, JSONObject.toJSONString(value));
    }

    public static void set(String key, String value) {
        redisService.set(key, value);
    }

    public static void del(String key) {
        redisService.del(key);
    }
    public static void del(String... key) {
        redisService.del(key);
    }

    public static <T> Set<T> smembers(Class<T> clazz, String key) {
        return redisService.smembers(key).stream().map(val ->
                (T) JSONUtil.parseStringToJavaBean(clazz, val)
        ).collect(Collectors.toSet());
    }
    public static Set smembers(String key) {
        return redisService.smembers(key);
    }
    public static void sadd(String key, Object... value) {
        Arrays.stream(value).map(val -> redisService.sadd(key, JSONUtil.toJSONString(val)));
    }
    public static Long scard(String key){
        return redisService.scard(key);
    }
    public static Long srem(String key, String... values){
        return redisService.srem(key,values);
    }
    public static Long srem(String key, Object... values){
        return Arrays.stream(values).mapToLong(value -> redisService.srem(key,JSONUtil.toJSONString(value))).sum();
    }

    public static String hget(String key, String name) {
        return redisService.hget(key, name);
    }

    public static <T> T hget(Class<T> clazz, String key, String name) {
        String result = redisService.hget(key, name);
        return JSONUtil.parseStringToJavaBean(clazz, Strings.isEmpty(result) ? "{}" : result);
    }

    public static void hset(String key, String name, Object value) {
        redisService.hset(key, name, JSONUtil.toJSONString(value == null ? "{}" : value));
    }

    public static void hset(String key, String name, String value) {
        redisService.hset(key, name, value);
    }

    public static void hdel(String key, String name) {
        redisService.hdel(key, name);
    }

    public static void hdel(String key, String... names) {
        redisService.hdel(key, names);
    }

    public static void lpush(String key, String... strings) {
        redisService.lpush(key,strings);
    }

    public static List<String> lrange(String key, long start, long end) {
        return redisService.lrange(key,start,end);
    }

    public static Map<String, String> hgetAll(String key) {
        return redisService.hgetAll(key);
    }

    public static List<String> scan(String pattern) {
        ScanParams scanParams = new ScanParams();
        scanParams.match(pattern);
        scanParams.count(Integer.MAX_VALUE);
        return redisService.scan("0", scanParams).getResult();
    }

    public static void flushDB(){
        redisService.flushDB();
    }
}